package com.qst.express.utils.write;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.qst.express.utils.DataDemo;
import com.qst.express.utils.TestFileUtil;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * @author:CoolTiger
 * @title: FillWrite 填充数据
 * @projectName springboot-e
 * @description: TODO
 * @date:2023/10/23/22:20:52
 **/
public class FillWrite {
    private static List<DataDemo> data(int count) {
        List<DataDemo> list = ListUtils.newArrayList();
        for (int i = 0; i < count; i++) {

            list.add(new DataDemo("马云"+i,i));
        }
        return list;
    }

    //批量写100万
    @Test
    public void write(){
        // 方案2 分多次 填充 会使用文件缓存（省内存）

        String fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
        String templateFileName = TestFileUtil.getPath() + "模板.xlsx";
        try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            long t1 = System.currentTimeMillis();
                excelWriter.fill(data(10), writeSheet);
            long t2 = System.currentTimeMillis();
            System.out.println(t2 - t1);
        }
    }

    public static void main(String[] args) {
        // 方案2 分多次 填充 会使用文件缓存（省内存）

        String fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
        String templateFileName = TestFileUtil.getPath() + "模板.xlsx";
        try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            long t1 = System.currentTimeMillis();
            excelWriter.fill(data(10), writeSheet);
            excelWriter.fill(data(10), writeSheet);
            long t2 = System.currentTimeMillis();
            System.out.println(t2 - t1);
        }
    }
}
